package com.infore.maintenance.main.controller;

import cn.hutool.json.JSONUtil;
import com.infore.common.enums.ExecuteSql;
import com.infore.common.fw.BaseController;
import com.infore.common.response.ApiEntityResponse;
import com.infore.common.response.ApiListResponse;
import com.infore.common.response.ApiPageResponse;
import com.infore.maintenance.main.service.CrudHandleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Api(value = "数据增删改查操作", tags = "数据增删改查操作")
@RequestMapping("/crudHandle")
public class CrudHandleController extends BaseController {


    @Autowired
    private CrudHandleService crudHandleService;


    /**
     * Controller接口层只能存在一个@RequestBody，如需支持多个@RequestBody需重写方法参数解析器
     * @param databaseSchema
     * @param tableName
     * @param columnPropertyJson
     * @param jsonArrayData
     * @return
     */
    @PostMapping("/insertTableListDynamic")
    @ResponseBody
    @ApiOperation(value = "批量数据动态插入", notes = "批量数据动态插入")
    public ApiEntityResponse<Integer> insertTableListDynamic(@RequestParam @ApiParam(required = true, name = "databaseSchema", value = "库名")  String databaseSchema,
                                                             @RequestParam @ApiParam(required = true, name = "tableName", value = "表名") String tableName,
                                                             @RequestParam @ApiParam(required = true, name = "columnPropertyJson", value = "key为数据表的列名，value为jsonArrayData数据中的key值") String columnPropertyJson,
                                                             @RequestParam @ApiParam(required = true, name = "jsonArrayData", value = "数据")  String jsonArrayData) {
        Map<String,String> columnProperty = JSONUtil.toBean(columnPropertyJson,Map.class);
        int rows = crudHandleService.insertTableListDynamic(databaseSchema, tableName, columnProperty, jsonArrayData);
        return ApiEntityResponse.ok(rows);
    }



    @PostMapping("/insertTableEntityDynamic")
    @ResponseBody
    @ApiOperation(value = "单行数据动态插入", notes = "单行数据动态插入")
    public ApiEntityResponse<Integer> insertTableEntityDynamic(@RequestParam @ApiParam(required = true, name = "databaseSchema", value = "库名")  String databaseSchema,
                                                                 @RequestParam @ApiParam(required = true, name = "tableName", value = "表名") String tableName,
//                                                                 @RequestBody  @ApiParam(required = true, name = "columnProperty", value = "key为数据表的列名，value为jsonArrayData数据中的key值") Map<String, String> columnProperty,
                                                                 @RequestParam  @ApiParam(required = true, name = "columnPropertyJson", value = "key为数据表的列名，value为jsonData数据中的key值") String columnPropertyJson,
                                                                 @RequestParam @ApiParam(required = true, name = "jsonData", value = "数据")  String jsonData) {

        Map<String,String> columnProperty = JSONUtil.toBean(columnPropertyJson,Map.class);
        int rows = crudHandleService.insertTableEntityDynamic(databaseSchema, tableName, columnProperty, jsonData);
        return ApiEntityResponse.ok(rows);
    }
    @PostMapping("/updateTableEntityDynamic")
    @ResponseBody
    @ApiOperation(value = "数据动态修改", notes = "数据动态修改")
    public ApiEntityResponse<Integer> updateTableEntityDynamic(@RequestParam @ApiParam(required = true, name = "databaseSchema", value = "库名")  String databaseSchema,
                                                               @RequestParam @ApiParam(required = true, name = "tableName", value = "表名") String tableName,
                                                               @RequestParam @ApiParam(required = true, name = "setColumnAndValueJson", value = "key为set数据表的列名，value为需要修改的值") String setColumnAndValueJson,
                                                               @RequestParam @ApiParam(required = true, name = "whereColumnAndValueJson", value = "key为where数据表的列名，value为where条件的值")  String whereColumnAndValueJson) {

        Map<String,String> setColumnAndValue = JSONUtil.toBean(setColumnAndValueJson,Map.class);
        Map<String,String> whereColumnAndValue = JSONUtil.toBean(whereColumnAndValueJson,Map.class);
        int rows = crudHandleService.updateTableEntityDynamic(databaseSchema, tableName, setColumnAndValue, whereColumnAndValue);
        return ApiEntityResponse.ok(rows);
    }


    @PostMapping("/deleteTableEntityDynamic")
    @ResponseBody
    @ApiOperation(value = "数据动态删除", notes = "数据动态删除")
    public ApiEntityResponse<Integer> deleteTableEntityDynamic(@RequestParam @ApiParam(required = true, name = "databaseSchema", value = "库名")  String databaseSchema,
                                                               @RequestParam @ApiParam(required = true, name = "tableName", value = "表名") String tableName,
                                                               @RequestParam @ApiParam(required = true, name = "whereColumnAndValueJson", value = "key为where数据表的列名，value为where条件的值")  String whereColumnAndValueJson) {

        Map<String,String> whereColumnAndValue = JSONUtil.toBean(whereColumnAndValueJson,Map.class);
        int rows = crudHandleService.deleteTableEntityDynamic(databaseSchema, tableName, whereColumnAndValue);
        return ApiEntityResponse.ok(rows);
    }


    @PostMapping("/insertBysql")
    @ResponseBody
    @ApiOperation(value = "执行数据插入SQL", notes = "执行数据插入SQL")
    public ApiEntityResponse<Integer> insertBysql(@RequestParam @ApiParam(required = true, name = "sql", value = "执行sql")  String sql) {
        int rows = crudHandleService.executeBySql(ExecuteSql.INSERT,sql);
        return ApiEntityResponse.ok(rows);
    }


    @PostMapping("/updateBysql")
    @ResponseBody
    @ApiOperation(value = "执行数据更新SQL", notes = "执行数据更新SQL")
    public ApiEntityResponse<Integer> updateBysql(@RequestParam @ApiParam(required = true, name = "sql", value = "执行sql")  String sql) {
        int rows = crudHandleService.executeBySql(ExecuteSql.UPDATE,sql);
        return ApiEntityResponse.ok(rows);
    }

    @PostMapping("/updateBysqlAndParameter")
    @ResponseBody
    @ApiOperation(value = "执行数据更新SQL-带参数", notes = "执行数据更新SQL-带参数")
    public ApiEntityResponse<Integer> updateBysqlAndMap(@RequestParam @ApiParam(required = true, name = "sql", value = "执行sql")  String sql,
                                                        @RequestBody  @ApiParam(required = true, name = "keyValueJson", value = "key为占位符名称，value为占位符需替换的值") String keyValueJson) {
        Map<String,Object> keyValue = JSONUtil.toBean(keyValueJson,Map.class);
        int rows = crudHandleService.executeBySql(ExecuteSql.UPDATE,sql,keyValue);
        return ApiEntityResponse.ok(rows);
    }



    @PostMapping("/deleteBysql")
    @ResponseBody
    @ApiOperation(value = "执行数据删除SQL", notes = "执行数据删除SQL")
    public ApiEntityResponse<Integer> deleteBysql(@RequestParam @ApiParam(required = true, name = "sql", value = "执行sql")  String sql) {
        int rows = crudHandleService.executeBySql(ExecuteSql.DELETE,sql);
        return ApiEntityResponse.ok(rows);
    }

    @PostMapping("/deleteBysqlAndParameter")
    @ResponseBody
    @ApiOperation(value = "执行数据删除SQL-带参数", notes = "执行数据删除SQL-带参数")
    public ApiEntityResponse<Integer> deleteBysqlAndParameter(@RequestParam @ApiParam(required = true, name = "sql", value = "执行sql")  String sql,
                                                              @RequestBody  @ApiParam(required = true, name = "keyValueJson", value = "key为占位符名称，value为占位符需替换的值") String keyValueJson) {
        Map<String,Object> keyValue = JSONUtil.toBean(keyValueJson,Map.class);
        int rows = crudHandleService.executeBySql(ExecuteSql.DELETE,sql,keyValue);
        return ApiEntityResponse.ok(rows);
    }

    @PostMapping("/selectBysql")
    @ResponseBody
    @ApiOperation(value = "执行数据查询SQL", notes = "执行数据查询SQL")
    public ApiListResponse<Map<String, Object>> selectBysql(@RequestParam @ApiParam(required = true, name = "sql", value = "执行sql")  String sql) {
        return crudHandleService.queryBySql(sql);
    }



    @PostMapping("/selectPageBysql")
    @ResponseBody
    @ApiOperation(value = "执行数据查询SQL-带分页", notes = "执行数据查询SQL-带分页")
    public ApiPageResponse<Map<String, Object>> selectPageBysql(@RequestParam @ApiParam(required = true, name = "sql", value = "执行sql")  String sql,
                                                                @RequestParam @ApiParam(required = true, name = "page", value = "当前页数")  Integer page,
                                                                @RequestParam @ApiParam(required = true, name = "limit", value = "分页大小")  Integer limit) {
        return crudHandleService.queryPageBySql(sql,page,limit);
    }


}
